<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/8/3
 * Time: 17:29
 */

namespace app\model\video;


use app\model\BaseModel;
use support\Db;

class VideoPartModel extends BaseModel
{
    protected $table = 'ting_video_part';

    protected $primaryKey = 'partid';

    const CREATED_AT = 'createtime';

    const UPDATED_AT = 'updatetime';

    protected static $instance;

    public static function getInstance()
    {
        if(is_object(self::$instance)){
            return self::$instance;
        }

        self::$instance = new self();

        return self::$instance;
    }

    public function index($where, $field=[],$order = [])
    {
        $query = $this->getWhere($where);

        $field && $query->select($field);

        $data = $this->buildPage($query);
        $order && is_array($order) && $query->orderBy($order[0],$order[1]);
        $order && is_string($order) && $query->orderBy(Db::raw($order));
        $query->orderBy('partid','desc');

        $data['data'] = $query->get();

        return $data;
    }

    public function getList($param)
    {
        $where = [
            'isdel'=>0,
            'status'=>1
        ];

        $query = $this->where($where);
        $data['data'] = $query->orderBy('createtime','desc')->get();

        return $data;
    }

    public function getDetail($where,$field=[])
    {
        $query = $this->where($where);
        $field && $detail = $query->select($field);
        $detail =  $query->first();

        if($detail==false){
            throw new UserException(4000);
        }

        return $detail;
    }

    public function saveData($param)
    {
        if(!empty($param['partid'])){
            $info = $this->find($param['partid']);
            if($info==false){
                throw new UserException(4010);
            }

            $data = [];
            !empty($param['title']) && $data['title'] = $param['title'];
            !empty($param['image']) && $data['image'] = $param['image'];
            !empty($param['video_mp4']) && $data['video_mp4'] = $param['video_mp4'];
            !empty($param['type']) && $data['type'] = $param['type'];

            isset($param['sort']) && $data['sort'] = $param['sort'];

            !empty($param['isdel']) && $data['isdel'] = $param['isdel'];
            if(isset($param['status'])){
                $data['status'] = $param['status']==1 || strtolower($param['status'])=='on'? 1: 0;
            }
            $data['updatetime'] = date('Y-m-d H:i:s');
            $this->where(['partid'=>$param['partid']])->update($data);

            if(!empty($param['video_mp4']) && $info['video_mp4']!=$param['video_mp4']){
                VideoPartCronModel::getInstance()->insert(
                    [
                        'partid'=>$info['partid'],
                        'video_mp4'=>$param['video_mp4'],
                        'createtime'=>date('Y-m-d H:i:s'),
                    ]
                );
            }
            return $info['partid'];

        }else{
            $data = [];
            $data['title'] = $param['title'];
            $data['image'] = $param['image'];
            $data['type'] = $param['type'];
            $data['video_mp4'] = $param['video_mp4'];
            $data['status'] = 0;

            $data['createtime'] = date('Y-m-d H:i:s');
            $data['updatetime'] = date('Y-m-d H:i:s');

            $id = $this->insertGetId($data);

            if($id){
                VideoPartCronModel::getInstance()->insert(
                    [
                        'partid'=>$id,
                        'video_mp4'=>$param['video_mp4'],
                        'createtime'=>date('Y-m-d H:i:s'),
                    ]
                );
            }
            return $id;
        }
    }

    public function deleteData($param)
    {
        $detail = $this->find($param['partid']);
        if($detail==false){
            throw new UserException(4003);
        }

        $update = [
            'isdel'=>1,
            'updatetime'=>date('Y-m-d H:i:s')
        ];

        $result = $this->where(['partid'=>$param['partid']])->update($update);
        return $result;
    }
}